package fuzzyController;

import net.sourceforge.jFuzzyLogic.FIS;

public class FuzzyController {
	public static final String CONTROLLERPATH = "fcl/airconditioner.fcl";
	public FIS fis;
	public FuzzyController(String path) throws Exception {
		fis = FIS.load(path, true);
		if (fis == null) {
			throw new Exception(String.format("Couldn't initialize fuzzy controller. Check if file %s exist", path));
		}
	}
	
	public FuzzyController() throws Exception {
		this(CONTROLLERPATH);
		
	}

	public void setTemperatureDifference(double difference) {
		fis.setVariable("temperatureDifference", difference);
	}
	
	public void setHeat(double heat) {
		fis.setVariable("heat", heat);
	}
	
	public State getHeaterState() {
		double value = fis.getVariable("heater").getValue();
		return ((int)Math.round(value))==1? State.ON:State.OFF;
	}
	
	public State getACState() {
		double value = fis.getVariable("ac").getValue();
		return ((int)Math.round(value))==1? State.ON:State.OFF;
	}
	
	public Double getBlow() {
		return fis.getVariable("blow").getValue();
	}
	
	public void evaluate() {
		fis.evaluate();
	}
	
	public static void main(String args[]) {
		FuzzyController fuzzyController = null;
		try {
			fuzzyController = new FuzzyController();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		fuzzyController.setTemperatureDifference(-2d);
		fuzzyController.setHeat(1);
		fuzzyController.evaluate();
		//fuzzyController.fis.chart();
		System.out.println(fuzzyController.fis);
		System.out.println("Heater:" + fuzzyController.getHeaterState());
		System.out.println("AC:" + fuzzyController.getACState());
		System.out.println("Blow:" + fuzzyController.getBlow());
	}

}
